$('#excel-file').change(function (e) {
	var customers;
	var file = e.target.files[0];
	var fileReader = new FileReader();
	fileReader.readAsBinaryString(file); // 以二进制方式打开文件
	fileReader.onload = function (ev) {
		try {
			var data = ev.target.result;
			workbook = XLSX.read(data, {
				type: 'binary'
			}); // 以二进制流方式读取得到整份excel表格对象
			customers = []; // 存储获取到的数据
		} catch (e) {
			chrome.runtime.sendMessage({
				key: 'loadError'
			});
			return;
		}

		// 表格的表格范围，可用于判断表头是否数量是否正确
		var fromTo = '';
		// 遍历每张表读取
		for (var sheet in workbook.Sheets) {
			if (workbook.Sheets.hasOwnProperty(sheet)) {
				fromTo = workbook.Sheets[sheet]['!ref'];
				customers = customers.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
				break; // 只取第一张表
			}
		}

		var json_obj = JSON.stringify(customers); //转化为JSON对象

		chrome.runtime.sendMessage({
			key: 'load',
			value: json_obj
		});
	};
});

//向content发消息
function sendMessageToContentScript(message, callback) {
	chrome.tabs.query({
		active: true
	}, function (tabs) {
		chrome.tabs.sendMessage(tabs[0].id, message, function (response) {
			if (callback) callback(response);
		});
	});
}

//当  前
$("#thisBtn").click(function () {
	chrome.runtime.sendMessage({
		key: 'this'
	}, function (response) {});
})

//上一条
$("#prevBtn").click(function () {
	chrome.runtime.sendMessage({
		key: 'prev'
	}, function (response) {});
})

//下一条
$("#nextBtn").click(function () {
	chrome.runtime.sendMessage({
		key: 'next'
	}, function (response) {});
})

//查  找
$("#lookupBtn").click(function () {
	var number = document.getElementById("lookup").value;
	if (number >= 2) {
		chrome.runtime.sendMessage({
			key: 'lookup',
			value: number
		});
	} else {
		return;
	}
})